package com.springboot.jiangan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.jiangan.Model.t_part;
import com.springboot.jiangan.Service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PartController {

    @Autowired
    private PartService jiangAnService;


    @GetMapping("/indexdepartment/{p_is}")
    public Map<String,Object> select(int page, int limit,String p_name,String p_remark,@PathVariable int p_is) {
        PageHelper.startPage(page, limit);
        t_part part=new t_part();
        part.setP_is(p_is);
//        System.out.println(p_id);
        if (!StringUtils.isEmpty(p_name)) {
            part.setP_name(p_name);
        }
        if (!StringUtils.isEmpty(p_remark)) {
            part.setP_remark(p_remark);
        }
        List<t_part> list = jiangAnService.selectJiangAn(part);
        PageInfo<t_part> pageInfo = new PageInfo<>(list);
        HashMap<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("msg","查询成功");
        result.put("data",pageInfo.getList());
        result.put("count",pageInfo.getTotal());
        return result;
    }

    @PostMapping("/indexdepartment")
    public Map<String, Object> add(@RequestBody t_part part) {
        jiangAnService.insertJiangAn(part);
        //插入成功
        HashMap<String, Object> result = new HashMap<>();
        result.put("message", "插入成功");
        result.put("status", "0");
        return result;
    }

    @PutMapping("/indexdepartment")
    public Map<String, Object> update(@RequestBody t_part part) {
        jiangAnService.updateJiangAn(part);
        //插入成功
        HashMap<String, Object> result = new HashMap<>();
        result.put("message", "修改成功");
        result.put("status", "0");
        return result;
    }
    @DeleteMapping("/indexdepartment/{p_id}/{p_is}")
    public Map<String,Object> delete(@PathVariable("p_id") int p_id,@PathVariable("p_is") int p_is) {
        t_part part=new t_part();
        part.setP_is(p_is);
        part.setP_id(p_id);
                jiangAnService.deleteJiangAn(part);
        HashMap<String,Object> result=new HashMap<>();
        result.put("message", "执行成功");
        result.put("status", "0");
        return result;
    }

//    @GetMapping("/indexdepartment/{p_id}")
//    public Map<String,Object> selectId(@PathVariable("p_id") int p_id) {
//        t_part list = jiangAnService.selectId(p_id);
//        HashMap<String,Object> result=new HashMap<>();
//        result.put("code",0);
//        result.put("msg","查询成功");
//        result.put("data",list);
//        result.put("count",1);
//        return result;
//    }
}
